package com.sinodata.bsm.cicp.finder.cics;

import java.io.BufferedReader;
import java.io.IOException;
import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.finder.linux.LinuxFinder;
import com.sinodata.bsm.cicp.util.CommandReader;

public abstract class CICSFinder extends Finder {
    private static final Logger logger = Logger.getLogger(LinuxFinder.class);
    private static final String HOSTNAME_CMDSTR = "hostname";
    private String HOSTNAME = "";

    /**
     * 获取主机名.在CICS资源发现时,将设置资源描述为:资源描述 +" for "+主机名
     */
    final String getHostName(String ip) {
        if (!HOSTNAME.equals("")) {
            return HOSTNAME;
        }
        BufferedReader reader = null;
        String strLine = null;
        try {
            reader = CommandReader.getReader(ip, HOSTNAME_CMDSTR, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                return strLine.trim();
            }
        } catch (IOException e) {
            logger.error("failed to get hostname ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return HOSTNAME;
    }

}
